﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Repositorys
{
    public class OssCompanyRouteReponsitory : SqlServerRespository<OssCompanyRouteEntity>, IOssCompanyRouteReponsitory
    {
        public OssCompanyRouteReponsitory()
        {
            Context = new OssContext();
        }

        public IList<OssCompanyRouteEntity> GetAllRoutes()
        {
            return GetAll().ToList();
        }

        public IList<OssCompanyRouteEntity> GetRoutesByCompanykey(string companykey)
        {
            var querysql = DapperHelper.CreateColSqlByMap<OssCompanyRouteEntity>();
            var sql = $@"SELECT {querysql} FROM oss_company_route
            WHERE zlcompanykey=@companykey";
            var result = GetList(sql, new {companykey}).ToList();
            return result;
        }

        public void AddRoute(OssCompanyRouteEntity entity, UnitOfWork unit)
        {
            Add(entity, unit);
        }

        public void RemoveRoute(OssCompanyRouteEntity entity, UnitOfWork unit)
        {
            Remove(entity, unit);
        }

        public void RemoveRouteByCompanykey(string companykey, UnitOfWork unit)
        {
            DapperHelper.Execute("delete from oss_company_route where zlcompanykey=@companykey", Context, unit,
                new {companykey});
        }
        /// <summary>
        /// 根据租户模块删除多余模块的路由
        /// </summary>
        /// <param name="companykey"></param>
        /// <param name="unit"></param>
        public void SyncRouteByMoudule(string companykey, UnitOfWork unit)
        {
            //TODO 
            DapperHelper.Execute("delete from oss_company_route where zlcompanykey=@companykey", Context, unit,
                new { companykey });
        }
    }
}